package com.youlai.boot.modules.product.controller;

import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.modules.product.model.ProductForm;
import com.youlai.boot.modules.product.model.ProductVO;
import com.youlai.boot.modules.product.model.ProductDetailVO;
import com.youlai.boot.modules.product.model.ProductShippingVO;
import com.youlai.boot.modules.product.service.ProductService;
import com.youlai.boot.modules.product.model.ProductCountryPageQuery;
import com.youlai.boot.modules.country.model.vo.CountryVO;
import com.youlai.boot.modules.category.model.CategoryVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.boot.modules.product.model.ProductPageQuery;

import java.util.List;

@Tag(name = "商品接口")
@RestController
@RequestMapping("/api/v1/product")
@RequiredArgsConstructor
public class ProductController {

    private final ProductService productService;

    @Operation(summary = "商品分页列表")
    @GetMapping("/page")
    public PageResult<ProductVO> listProducts(ProductPageQuery query) {
        Page<ProductVO> result = productService.getProductPage(query);
        return PageResult.success(result);
    }

    @Operation(summary = "商品详情")
    @GetMapping("/{id}")
    public Result<ProductDetailVO> getProduct(@PathVariable Long id) {
        ProductDetailVO product = productService.getProductById(id);
        return Result.success(product);
    }

    @Operation(summary = "商品货运信息")
    @GetMapping("/{id}/shipping")
    public Result<ProductShippingVO> getProductShipping(@PathVariable Long id) {
        ProductShippingVO shipping = productService.getProductShipping(id);
        return Result.success(shipping);
    }

    @Operation(summary = "添加商品")
    @PostMapping
    public Result<Long> addProduct(@Valid @RequestBody ProductForm product) {
        Long id = productService.saveProduct(product);
        return Result.success(id);
    }

    @Operation(summary = "修改商品")
    @PutMapping("/{id}")
    public Result<Boolean> updateProduct(
            @PathVariable Long id,
            @Valid @RequestBody ProductForm product) {
        product.setId(id);
        productService.updateProduct(product);
        return Result.success(true);
    }

    @Operation(summary = "删除商品")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteProduct(@PathVariable Long id) {
        boolean result = productService.deleteProduct(id);
        return Result.judge(result);
    }

    @Operation(summary = "获取商品分类")
    @GetMapping("/{productId}/categories")
    public Result<List<CategoryVO>> getProductCategories(@PathVariable Long productId) {
        List<CategoryVO> categories = productService.getProductCategories(productId);
        return Result.success(categories);
    }

    @Operation(summary = "添加商品分类")
    @PostMapping("/{productId}/category/{categoryId}")
    public Result<Boolean> addProductCategory(@PathVariable Long productId, @PathVariable Long categoryId) {
        productService.saveProductCategory(productId, categoryId);
        return Result.success(true);
    }

    @Operation(summary = "删除商品分类")
    @DeleteMapping("/{productId}/category/{categoryId}")
    public Result<Boolean> deleteProductCategory(@PathVariable Long productId, @PathVariable Long categoryId) {
        productService.deleteProductCategory(productId, categoryId);
        return Result.success(true);
    }

    @Operation(summary = "获取商品国家")
    @GetMapping("/{productId}/countries")
    public PageResult<CountryVO> getProductCountries(@PathVariable Long productId, ProductCountryPageQuery query) {
        Page<CountryVO> result = productService.getProductCountryPage(productId, query);
        return PageResult.success(result);
    }

    @Operation(summary = "查询商品国家")
    @GetMapping("/{productId}/country/{countryCode}")
    public Result<CountryVO> getProductCountry(@PathVariable Long productId, @PathVariable String countryCode) {
        CountryVO country = productService.getProductCountry(productId, countryCode);
        return Result.success(country);
    }

    @Operation(summary = "添加商品国家")
    @PostMapping("/{productId}/country/{countryCode}")
    public Result<Boolean> addProductCountry(@PathVariable Long productId, @PathVariable String countryCode) {
        productService.saveProductCountry(productId, countryCode);
        return Result.success(true);
    }

    @Operation(summary = "删除商品国家")
    @DeleteMapping("/{productId}/country/{countryCode}")
    public Result<Boolean> deleteProductCountry(@PathVariable Long productId, @PathVariable String countryCode) {
        productService.deleteProductCountry(productId, countryCode);
        return Result.success(true);
    }
}
